package com.recycle.recycledemo.recycle;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by lwg on 2017/12/22.
 */

public class CommonViewHolder extends RecyclerView.ViewHolder {

    private Context context;
    private View converView;
    private SparseArray<View> views;

    public CommonViewHolder(Context context, View itemView, ViewGroup viewGroup) {
        super(itemView);
        this.context = context;
        this.converView = itemView;
        views = new SparseArray<View>();
    }

    public static CommonViewHolder getInstance(Context context,int layoutId,ViewGroup viewGroup){
        View itemView= View.inflate(context, layoutId,viewGroup);
        CommonViewHolder viewHolder = new CommonViewHolder(context,itemView,viewGroup);
        return viewHolder;
    }

    public <T extends View> T getView(int viewId){
        View view = views.get(viewId);
        if(view == null){
            view = itemView.findViewById(viewId);
            views.put(viewId,view);
        }
        return (T)view;
    }
}
